package com.spzx.common.core.utils;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/*
    为当前的项目提供线程池
 */
public class ThreadPoolUtils {
    //构造器私有化，线程池对象由我们创建，只创建一次(单例)
    private ThreadPoolUtils(){

    }
    private static ThreadPoolExecutor executor;

    public static ThreadPoolExecutor getPool(){
        //如果线程池不为空 直接返回线程池(不加锁)
        if(executor!=null){
            return executor;
        }
        synchronized (ThreadPoolUtils.class){
            if(executor!=null){
                return executor;
            }
            //核心线程数： 可以使用  cpu核心数/1-0.9
            int processors = Runtime.getRuntime().availableProcessors();
            executor = new ThreadPoolExecutor(processors*10 , 300 , 60 , TimeUnit.SECONDS,
                    new ArrayBlockingQueue<>(600) ,
                    Executors.defaultThreadFactory());
            return executor;
        }


    }
}
